//
//  TutorialViewController.m
//  TutorialsModule
//
//  Created by Augusto Guido on 29/09/09.
//  Copyright 2009 Moove-IT. All rights reserved.
//

#import "TutorialViewController.h"
#import "EditTutorialViewController.h"
#import "StepsTableViewController.h"
#import "PreviewTutorialView.h"
#import "NewTutorialViewController2.h"
#import "FBCTutorialsLogin.h"


@implementation TutorialViewController

@synthesize session;

/* FACEBOOK CONNECT*/

-(void)session:(FBSession *)session didLogin:(FBUID)uid
{
	NSLog(@"lalala");
	
}

/**
 * Called when a session is about to log out.
 */
- (void)session:(FBSession*)session willLogout:(FBUID)uid{}

/**
 * Called when a session has logged out.
 */
- (void)sessionDidLogout:(FBSession*)session{}

/* FIN FBC */


- (IBAction)previewTutorial {
    PreviewTutorialView *previewView = [[PreviewTutorialView alloc] initWithNibName:@"PreviewTutorialView" bundle:nil];	
	[self presentModalViewController:previewView animated:YES];
	[previewView release];
}


- (IBAction)viewSteps 
{
	StepsTableViewController *stepsViewController = [[StepsTableViewController alloc] initWithNibName:@"StepsTableViewController" bundle:nil objects:nil];
	[self.navigationController pushViewController:stepsViewController animated:YES];
	[stepsViewController release];
	
}

-(id) initWithNibName: (NSString*) nibNameOrNil bundle:(NSBundle *)nibBundleOrNil objects: (NSArray *)objects
{
	[self initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
	return self;
};


// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {        
		[self setHidesBottomBarWhenPushed: YES];
    }
    return self;
}



// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
    [super viewDidLoad];

	[session resume];
	
	UIBarButtonItem *edit = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editTutorial)];
	self.navigationItem.rightBarButtonItem = edit;
	
	[edit release];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

- (void) editTutorial
{
	
	NewTutorialViewController2 *editViewController = [[NewTutorialViewController2 alloc] initWithNibName:@"NewTutorialViewController2" bundle: nil];
//	UIBarButtonItem *done = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:editViewController action:@selector(editTutorial)];
//	editViewController.navigationItem.rightBarButtonItem = done;
		
	[self.navigationController pushViewController:editViewController animated:YES];
	
	[editViewController release];
	
	
	//[done release];
	
}


- (IBAction)publishTutorial {
	
	UIActionSheet *actionSheet = [[[UIActionSheet alloc] initWithTitle:@"Publish" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Facebook", @"Youtube", nil]autorelease];
	actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
	[actionSheet showInView:self.view];
	
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
	
	if (buttonIndex < 2) // el boton 2 es el cancel
	{
		
		if(buttonIndex == 0) //publicar en facebook 
		{
			
			//inicio la session con los datos de la app	
			// si esta conectado, puiblico
			if(session.isConnected) {
				//[session logout];
				NSLog(@"sigue conectado");
			}
			else
			{
				session = [FBSession sessionForApplication:@API_KEY secret:@API_SECRET delegate:self];
				
				FBLoginDialog* dialog = [[[FBLoginDialog alloc] initWithSession:session] autorelease];
				[dialog show];
			}			
			
			
			/*FBCTutorialsLogin *fbcTutorialslLogin = [[FBCTutorialsLogin alloc] initWithNibName:@"FBCTutorialsLogin" bundle:nil];
			[self.navigationController pushViewController:fbcTutorialslLogin animated:YES];
			
			[fbcTutorialslLogin release];*/
			
		}
		
	}
	
	/*if (buttonIndex < 2){
		UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init];
		imagePickerController.allowsEditing = YES;
		if (buttonIndex == 0)
			[imagePickerController setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
		if (buttonIndex == 1)
			if ( [UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
				[imagePickerController setSourceType:UIImagePickerControllerSourceTypeCamera];
		imagePickerController.delegate = self;		
		[self presentModalViewController:imagePickerController animated:YES];		
		[imagePickerController release];
	}*/	
}



- (void)dealloc {
    [super dealloc];
}


@end
